package gui.mainframe;

import gui.commons.CommonLayoutOperations;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;
import javax.swing.border.Border;

public class PreferencesDialog extends JDialog {

	/*
	 * field variables declaration
	 */
	private JButton ok, cancel;
	private JSpinner spPort;
	private SpinnerNumberModel spm;
	private JTextField tfUserName;
	private JPasswordField pfUserPass;
	private CommonLayoutOperations cOps;

	private PreferencesListener prl;

	/*
	 * layout variables declaration
	 */
	private int y;
	private GridBagConstraints gc;
	private Insets lblInset, tfInset, btnInset;
	Border titleBorder = BorderFactory.createTitledBorder("DB connection"),
			spaceBorder = BorderFactory.createEmptyBorder(10, 10, 10, 10);

	public PreferencesDialog(JFrame source, String title, boolean modal) {
		super(source, title, modal);

		/*
		 * init variables and set models
		 */
		initVariables();

		/*
		 * manage all layout
		 */
		manageLayout(source);

		/*
		 * listeners
		 */
		manageListeners();
	}
	
	public void initVariables() {
		ok = new JButton("Aceptar");
		cancel = new JButton("Cancelar");

		spm = new SpinnerNumberModel(80, 0, 9999, 1);
		spPort = new JSpinner(spm);

		tfUserName = new JTextField(10);
		pfUserPass = new JPasswordField(10);
		pfUserPass.setEchoChar('°');
	}

	public void manageLayout(JFrame source) {
		/*
		 * init layout variables and manage it
		 */
		gc = new GridBagConstraints();
		lblInset = new Insets(0, 0, 0, 1);
		tfInset = new Insets(0, 1, 0, 1);
		btnInset = new Insets(1, 1, 1, 1);

		/*
		 * Common tasks variable
		 */
		cOps = new CommonLayoutOperations(lblInset, tfInset, btnInset);

		JPanel controlPanel = new JPanel();
		JPanel buttonsPanel = new JPanel();

		controlPanel.setBorder(BorderFactory.createCompoundBorder(spaceBorder, titleBorder));
//		buttonsPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		controlPanel.setLayout(new GridBagLayout());
		buttonsPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));

		/*
		 * gc.gridy = 0; gc.gridx = 0;
		 */
		gc.weightx = 1;
		gc.weighty = 1;
		gc.fill = GridBagConstraints.NONE;

		y = 0;
		cOps.addJLabel(controlPanel, "Usuario: ", 0, y);
		cOps.addTextField(controlPanel, tfUserName, 1, y);
		y++;

		cOps.addJLabel(controlPanel, "Contraseña: ", 0, y);
		cOps.addJPasswordField(controlPanel, pfUserPass, 1, y);
		y++;

		cOps.addJLabel(controlPanel, "Puerto comunicación: ", 0, y);
		cOps.addJSpinner(controlPanel, spPort, 1, y);
		// y++;

		// cOps.addJButton(buttonsPanel, ok, 0, y);
		// cOps.addJButton(buttonsPanel, cancel, 1, y);

		buttonsPanel.add(ok);
		buttonsPanel.add(cancel);
		// and I set the buttons to be same size as the longest (cancel)
		ok.setPreferredSize(cancel.getPreferredSize());

		/*
		 * finally add the panels to the dialog
		 */
		setLayout(new BorderLayout());
		add(controlPanel, BorderLayout.CENTER);
		add(buttonsPanel, BorderLayout.SOUTH);

		setSize(425, 320);
		setMinimumSize(getSize());
		setLocationRelativeTo(source);

	}
	
	public void manageListeners() {

		ok.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// int portNumber = (Integer)spPort.getValue();
				// String uname = tfUserName.getText();
				// char[] pass = pfUserPass.getPassword();
				//
				// System.out.println("uname: " + uname + ", pass: " + pass +
				// ", parsed pass: " + new String(pass) + ", port: " +
				// portNumber);

				if (prl != null) {
					prl.savePreferences(tfUserName.getText(), new String(
							pfUserPass.getPassword()), (Integer) spPort
							.getValue());
					// dialogue dialog
				}

				setVisible(false);
			}
		});

		cancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// System.out.println("diag.vals - port: " + sp.getValue());
				setVisible(false);
			}
		});
	}

	public void setPreferencesListener(PreferencesListener prl) {
		this.prl = prl;
	}

	/*
	 * method to put in the default saved values
	 */
	public void setDefaults(String uname, String upass, int portNum) {
		tfUserName.setText(uname);
		pfUserPass.setText(upass);
		spPort.setValue(portNum);
	}

	/*
	 * method to export the preferences to an xml file
	 */
	public void exportPreferences(Preferences p, String str) {
		try {
			p.exportNode((OutputStream) new FileOutputStream("prefs.xml"));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (BackingStoreException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
